package com.jiajia.study.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @Title: RabbitDelayConfig
 * @Author zjj
 * @Package com.jiajia.study.config
 * @Date 2024/3/11 11:15
 * @description:
 */


@Configuration
@Slf4j
public class RabbitDelayConfig {


    @Bean
    public DirectExchange directExchangeNormal(){
        return ExchangeBuilder.directExchange("delay.exchange.normal").build();
    }

    @Bean
    public DirectExchange directExchangeDelay(){
        return ExchangeBuilder.directExchange("delay.exchange.delay").build();
    }

    @Bean
    public Queue queueNormal(){
        Map<String,Object> queueArguments = new HashMap<>();
        queueArguments.put("x-message-ttl",20*1000);
        queueArguments.put("x-dead-letter-exchange","delay.exchange.delay");
        //  队列中的消息过期，将消息投递到的队列时的路由key
        queueArguments.put("x-dead-letter-routing-key","delay");
        return QueueBuilder.durable("delay.queue.normal").withArguments(queueArguments).build();
    }

    @Bean
    public Queue queueDelay(){
        return QueueBuilder.durable("delay.queue.delay").build();
    }

    @Bean
    public Binding bindingNormal(DirectExchange directExchangeNormal, Queue queueNormal){
        return BindingBuilder.bind(queueNormal).to(directExchangeNormal).with("normal");
    }

    @Bean
    public Binding bindingDelay(DirectExchange directExchangeDelay,Queue queueDelay){
        log.info("message send success. send message time is {}",new Date());
        return BindingBuilder.bind(queueDelay).to(directExchangeDelay).with("delay");
    }
}
